/*
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the FxStruts MailReader Application.
 *
 * The Initial Developer of the Original Code is
 * Anirudh Sasikumar (http://anirudhs.chaosnet.org/).
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
*/

package com.adobe.mailreader.view
{
    import com.adobe.mailreader.events.NotifyViewEvent;
    import com.adobe.mailreader.events.ResourcesEvent;
    import com.adobe.mailreader.model.MailModelLocator;
    import com.adobe.cairngorm.control.CairngormEventDispatcher;
    import mx.controls.Alert;
    
    import flash.events.Event;
    
    public class MainViewMediator extends ViewMediator
    {
        private var _view:FxStrutsMailReader;
        
        public function MainViewMediator()
        {
            super();
            notificationInterests = [MailModelLocator.VIEW_LOGGED_IN_STATE,
                                     MailModelLocator.VIEW_LOGGED_OUT_STATE,
                                     MailModelLocator.VIEW_LOGIN_FAILED,
                                     MailModelLocator.MODEL_RESOURCES_GET_SUCCESS,
                                     MailModelLocator.MODEL_RESOURCES_GET_FAILED,
                                     MailModelLocator.MODEL_LOCALE_GET_SUCCESS,
                                     MailModelLocator.MODEL_LOCALE_GET_FAILED];
            
        }
        
        public function set view (viewIn:FxStrutsMailReader):void
        {
            _view = viewIn;
        }
        
        private function faultEffectEnd(evt:Event):void
        {

        }
        
        protected override function handleNotification(event:NotifyViewEvent):void
        {
            switch ( event.type )
            {
                case MailModelLocator.VIEW_LOGGED_IN_STATE:
                    
                    _view.currentState = 'main';
                    
                    break;
                    
                case MailModelLocator.VIEW_LOGGED_OUT_STATE:
                    
                    _view.currentState = 'login';
                    
                    break;
                    
                case MailModelLocator.VIEW_LOGIN_FAILED:
                    
                    break;
                    
                case MailModelLocator.MODEL_RESOURCES_GET_FAILED:
                    
                    Alert.show("Unable to load required resource file. Please try again later.");
                    
                    break;
                    
                case MailModelLocator.MODEL_RESOURCES_GET_SUCCESS:
                    
                    _view.currentState = 'login';                    
                    
                    break;
                    
                case MailModelLocator.MODEL_LOCALE_GET_FAILED:
                    
                    Alert.show("Unable to switch to requested locale. Please try again later.");
                    
                    break;
                    
                case MailModelLocator.MODEL_LOCALE_GET_SUCCESS:
                    
                    _view.currentState = '';               
                    _view.loginForm.registerButton.enabled = true;
                    _view.loginForm.loginButton.enabled = true;
                    
                    var evt:ResourcesEvent = new ResourcesEvent(ResourcesEvent.EVENT_RESOURCES_GET);
                    CairngormEventDispatcher.getInstance().dispatchEvent( evt );
                    
                    break;
                    
                    
            }
        }
        
    }
}